package com.boscloud.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Service
public class  postSapServices {
    @Value("${sap.testServer.username}")
    private  String username;
    @Value("${sap.testServer.password}")
    private  String password;
    @Value("${sap.testServer.url}/ZITMS")
    private  String url;
    @Value("${sap.testServer.client}")
    private  String client;
    /**
     * post 方法
     * @param params post 参数
     * */
    public  String post( Map<String, Object> params,Map<String, String> header) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
        headers.setBasicAuth(username,password);
        for( String headerKey:header.keySet()) {
            headers.add(headerKey,header.get(headerKey));
        }
        Map<String, Object> mapData = new HashMap<String, Object>();
        for (String key : params.keySet()) {
            mapData.put(key, params.get(key));
        }
        RestTemplate restTemplate = new RestTemplate();
        JSONObject jsonParams = new JSONObject(mapData);

        HttpEntity<String> strEntity = new HttpEntity<String>(jsonParams.toJSONString(), headers);
        String res = restTemplate.postForObject(url, strEntity, String.class);
        return res;
    }
}
